mod abi;
mod config;
pub mod pb;
pub use abi::Tpl;
use anyhow::Result;
pub use config::AppConfig;
use futures::Stream;
use pb::{
    metadata_server::{Metadata, MetadataServer},
    Content, MaterializeRequest,
};
use std::pin::Pin;
use tonic::{async_trait, Request, Response, Status};
type ServiceResult<T> = std::result::Result<Response<T>, Status>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<Content, Status>> + Send>>;

#[async_trait]
impl Metadata for MetadataService {
    type MaterializeStream = ResponseStream;
    async fn materialize(
        &self,
        request: Request<tonic::Streaming<MaterializeRequest>>,
    ) -> ServiceResult<Self::MaterializeStream> {
        let req = request.into_inner();
        self.materialize(req).await
    }
}
pub struct MetadataService {
    pub config: AppConfig,
}

impl MetadataService {
    pub fn new(config: AppConfig) -> Self {
        Self { config }
    }

    pub fn into_server(self) -> MetadataServer<Self> {
        MetadataServer::new(self)
    }
}
